﻿#ifndef CHANNELITEMVIEW_H
#define CHANNELITEMVIEW_H

#include <QBasicTimer>
#include <QListView>
#include <functional>
#include "channelinfo.h"
#include "channelitemmodel.h"

class ChatRoom;
class ContactUser;

class ChannelItemView : public QListView
{
    Q_OBJECT
public:
    explicit ChannelItemView(QWidget *parent = Q_NULLPTR);
    bool hasChannel(qint64 rid) const;
    const ChannelInfo *constFindChannel(qint64 rid) const;
    const P2PChanneInfo *constFindP2pChannel(qint64 uid) const;
    const BaseChannelInfo *findAllChannel(qint64 uniqueId) const;
    ChannelInfo *findChannel(qint64 rid);
    P2PChanneInfo *findP2pChannel(qint64 uid);

    void handle_login_ack(qint64 rid, QVariantHash &msg);
    ChatRoom *handle_open(qint64 uniqueId);

    void handle_join_or_exit(int cmd_type, qint64 rid, QVariantHash &msg);
    void handle_channel_list(const QList<QVariantHash> &channel_list);
    void handle_channel_msg(qint64 rid, QVariantHash &msg, bool isChannelOpen);
    void handle_channel_msg_list(const qint64 &rid, QList<QVariantHash> &msg_list);
    QList<QVariantHash> handle_mic_state(qint64 rid, QVariantHash &msg);
    void handle_basic_info(qint64 rid, QVariantHash &msg);
    void handle_contact_list();
    QList<QVariantHash> handle_mic_ack(qint64 rid, int code, const QString &action);
    inline void setMsgDirection(QVariantHash &msg);

    bool preprocess_p2p_msg(const qint64 &uid, int type, QVariantHash &msg);
    void process_p2p_special_msg(const qint64 &uid, int type, QVariantHash &msg, bool isRead);
    bool handle_p2p_media(const qint64 &uid, bool on);
    void handle_p2p_msg(const qint64 &uid, QVariantHash &msg, bool isRead);
    void handle_p2p_msg_list(const qint64 &uid, QList<QVariantHash> &msg_list);

    void delete_room_message(qint64 rid);
    void set_room_mute_list();
    void set_room_mute(qint64 rid, bool mute);
    void delete_p2p_message(qint64 uid);
    void set_user_mute_list();
    void set_user_mute(qint64 uid, bool mute);
    void set_mute_all(bool mute);

    void handle_search(const QString& keyword);

    std::function<const ContactUser *(qint64)> getContactUser;
signals:
    void deleteChannelMessage(qint64 uniqueId);
protected:
    void contextMenuEvent(QContextMenuEvent * e);
    void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>());
    void rowsInserted(const QModelIndex &parent, int start, int end);
private slots:
    void checkAllRowHidden();
private:
    void checkRowHidden(int start, int end, const QString &keyword);
    P2PChanneInfo *createP2pChannel(qint64 uid);
    ChannelItemModel *channelModel;
    QMap<qint64, ChannelInfo> map_channel;
    QMap<qint64, P2PChanneInfo> map_p2pchannel;
    QString searchword;
    const qint64 myuid;
};

#endif // CHANNELITEMVIEW_H
